/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seagate.app.servlet;

import com.seagate.app.bean.BoBean;
import com.seagate.app.bean.RoomBean;
import com.seagate.app.manager.LimitManager;
import com.seagate.app.manager.MailListManager;
import com.seagate.app.manager.RoomManager;
import com.seagate.util.mail.MailUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 452530
 */
public class RoomSettingServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        PrintWriter out = response.getWriter();
        try {

            String action = request.getParameter("action");
            if ("updateshelf".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                String productname = request.getParameter("productname");
                String bondpad = request.getParameter("bondpad");
                String tgatype = request.getParameter("tgatype");
                RoomManager rmg = new RoomManager();
                boolean success = rmg.updateShelf(shelfno, productname, tgatype, bondpad);
                out.println("{\"success\":" + success + "}");
            } else if ("checkshelfcap".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                String qty = request.getParameter("qty");
                RoomManager rmg = new RoomManager();
                int totalBoQty = rmg.getBoQtyByShelf(shelfno) + rmg.getBoQtyByShelf("F" + shelfno) + Integer.parseInt(qty);
                RoomBean roomcap = rmg.loadRoomShelfInfo(shelfno);
                if (roomcap.getCapacity() < totalBoQty) {
                    out.println("{\"success\":false}");
                } else {
                    out.println("{\"success\":true}");
                }
            } else if ("checkhgaboduplicate".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                RoomManager rmg = new RoomManager();
                BoBean bo = rmg.getRoomShelfBoByHgaBo(hgabo);
                if (bo == null) {
                    out.println("{\"success\":false}");
                } else {
                    out.println("{\"success\":true,");
                    if (shelfno.equalsIgnoreCase(bo.getRoom_shelf_no())) {
                        out.println("\"success2\":true,");
                    } else {
                        out.println("\"success2\":false,");
                    }
                    out.println("\"shelfno\":\"" + bo.getRoom_shelf_no() + "\"}");
                }
            } else if ("updatehagboqty".equalsIgnoreCase(action)) {
                String shelfno = request.getParameter("shelf");
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                String qty = request.getParameter("qty");
                String tsr = request.getParameter("tsr");
                String partnum = request.getParameter("partnum");
                String tab = request.getParameter("tab");
                RoomManager rmg = new RoomManager();
                boolean success = rmg.updateShelfBo(shelfno, hgabo, qty, tab, tsr, partnum);
                out.println("{\"success\":" + success + "}");
            } else if ("findBoData".equalsIgnoreCase(action)) {
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                RoomManager rmg = new RoomManager();
                BoBean bo = rmg.getBoEts(hgabo);
                if (bo == null) {
                    out.println("{\"success\":false}");
                } else {
                    out.println("{\"success\":true,");
                    out.println("\"boname\":\"" + bo.getBoname() + "\",");
                    out.println("\"tsr\":\"" + bo.getTsr() + "\",");
                    out.println("\"tab\":\"" + bo.getTab() + "\",");
                    out.println("\"partnum\":\"" + bo.getHga_partnum() + "\"");
                    out.println("}");
                }

            } else if ("addshelfbo".equalsIgnoreCase(action)) {
                String shelf = request.getParameter("shelf");
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                String qty = request.getParameter("qty");
                String tsr = request.getParameter("tsr");
                String partnum = request.getParameter("partnum");
                String tab = request.getParameter("tab");
                RoomManager rmg = new RoomManager();
                boolean success = rmg.addShelfBo(shelf, hgabo, tsr, partnum, tab, qty);
                out.println("{\"success\":" + success + "}");

            } else if ("loadBoData".equalsIgnoreCase(action)) {
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                String shelf = request.getParameter("shelf");
                RoomManager rmg = new RoomManager();
                BoBean bo = rmg.getRoomShelfBo(shelf, hgabo);
                if (bo == null) {
                    out.println("{\"success\":false}");
                } else {
                    out.println("{\"success\":true,");
                    out.println("\"boname\":\"" + bo.getBoname() + "\",");
                    out.println("\"tsr\":\"" + bo.getTsr() + "\",");
                    out.println("\"tab\":\"" + bo.getTab() + "\",");
                    out.println("\"qty\":\"" + bo.getQty() + "\",");
                    out.println("\"partnum\":\"" + bo.getHga_partnum() + "\"");
                    out.println("}");
                }

            } else if ("checkBoData".equalsIgnoreCase(action)) {
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                RoomManager rmg = new RoomManager();
                BoBean bo = rmg.getRoomShelfBoByHgaBo(hgabo);
                if (bo == null) {
                    out.println("{\"success\":false}");
                } else {
                    out.println("{\"success\":true,");
                    out.println("\"boname\":\"" + bo.getBoname() + "\",");
                    out.println("\"tsr\":\"" + bo.getTsr() + "\",");
                    out.println("\"tab\":\"" + bo.getTab() + "\",");
                    out.println("\"qty\":\"" + bo.getQty() + "\",");
                    out.println("\"partnum\":\"" + bo.getHga_partnum() + "\"");
                    out.println("}");
                }

            } else if ("rejecthagboqty".equalsIgnoreCase(action)) {
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                String qty = request.getParameter("qty");
                RoomManager rmg = new RoomManager();
                BoBean bo = rmg.getRoomShelfBoByHgaBo(hgabo);
                if (bo == null) {
                    out.println("{\"success\":false}");
                } else {
                    RoomBean rbean = rmg.loadRoomShelfInfo(bo.getRoom_shelf_no());
                    int totalQty = Integer.parseInt(bo.getQty()) - Integer.parseInt(qty);
                    boolean success = rmg.updateShelfBo(bo.getRoom_shelf_no(), hgabo, "" + totalQty, bo.getTab(), bo.getTsr(), bo.getHga_partnum());
                    if (success) {
                        String gid = (String) request.getSession().getAttribute("GID");
                        boolean success2 = rmg.addRejectLog(hgabo, qty, bo.getTab(), bo.getTsr(), bo.getHga_partnum(), rbean.getTga_type(), "" + rbean.getBond_pad(), gid);
                        LimitManager lm = new LimitManager();
                        int limit = lm.getLimit();
                        if (totalQty < limit) {
                            int limit2 = lm.getLimit2();
                            if (totalQty < limit2) {
                                MailListManager mlm = new MailListManager();
                                List maillist = mlm.getMailList();
                                String mailTo = "tanit.sittayakorn@seagate.com";
                                for (int i = 0; i < maillist.size(); i++) {
                                    mailTo = mailTo + "," + maillist.get(i) + "";
                                }
                                MailUtil sendmail = new MailUtil();
                                String bodymail = "<h1 style=\"color:red;\">Critical Alert Mail From AMA Standard Parts System</h1><br/>"
                                        + "<b>Standard parts of product</b> : <b style=\"color:red;\">" + rbean.getProductname() + " - " + rbean.getTga_type() + "</b><br/>"
                                        + "<b>in room shelf number</b>:<b style=\"color:red;\"> " + rbean.getShelf_no() + "</b><br/>"
                                        + "have quantity lower the limit.<br/>"
                                        + "It have only " + totalQty + " from total " + rbean.getCapacity() + "  ~= " + ((double) (((double) totalQty / ((double) rbean.getCapacity())) * 100)) + "% <br/>"
                                        + "<b style=\"color:red;\">Please increase standard parts quantity ,Now!!</b><br/>"
                                        + "";
                                try {
                                    sendmail.sendMail("Critical!! Alert Mail From AMA Standard Parts System ", bodymail, "itfis-Teparuk@seagate.com", mailTo);
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            } else {
                                MailListManager mlm = new MailListManager();
                                List maillist = mlm.getMailList();
                                String mailTo = "tanit.sittayakorn@seagate.com";
                                for (int i = 0; i < maillist.size(); i++) {
                                    mailTo = mailTo + "," + maillist.get(i) + "";
                                }
                                MailUtil sendmail = new MailUtil();
                                String bodymail = "<h1 style=\"color:#FF9900;\">Warning Alert Mail From AMA Standard Parts System</h1><br/>"
                                        + "<b>Standard parts of product</b> : <b style=\"color:red;\">" + rbean.getProductname() + " - " + rbean.getTga_type() + "</b><br/>"
                                        + "<b>in room shelf number</b>:<b style=\"color:red;\"> " + rbean.getShelf_no() + "</b><br/>"
                                        + "have quantity lower the limit.<br/>"
                                        + "It have only " + totalQty + " from total " + rbean.getCapacity() + "  ~= " + ((double) (((double) totalQty / ((double) rbean.getCapacity())) * 100)) + "% <br/>"
                                        + "<b style=\"color:red;\">Please increase standard parts quantity!!</b><br/>"
                                        + "";
                                try {
                                    sendmail.sendMail("Warning!! Alert Mail From AMA Standard Parts System ", bodymail, "itfis-Teparuk@seagate.com", mailTo);
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                        System.out.println("success2:" + success2);
                        if (success2) {
                            BoBean failbo = rmg.getRoomShelfBo("F" + bo.getRoom_shelf_no(), hgabo);

                            boolean success3 = false;
                            if (failbo == null) {
                                success3 = rmg.addShelfBo("F" + bo.getRoom_shelf_no(), hgabo, bo.getTsr(), bo.getHga_partnum(), bo.getTab(), qty);
                            } else {
                                int totalFailQty = Integer.parseInt(failbo.getQty()) + Integer.parseInt(qty);
                                success3 = rmg.updateShelfBo(failbo.getRoom_shelf_no(), hgabo, "" + totalFailQty, failbo.getTab(), failbo.getTsr(), failbo.getHga_partnum());
                            }

                            if (success3) {
                                out.println("{\"success\":true,");
                                out.println("\"shelf\":\"" + bo.getRoom_shelf_no() + "\"}");
                            } else {
                                out.println("{\"success\":false}");
                            }
                        } else {
                            out.println("{\"success\":false}");
                        }
                    } else {
                        out.println("{\"success\":false}");
                    }

                }

            } else if ("deleteBoData".equalsIgnoreCase(action)) {
                String hgabo = request.getParameter("hgabo");
                hgabo = hgabo.toUpperCase();
                String shelf = request.getParameter("shelf");
                RoomManager rmg = new RoomManager();

                boolean success = rmg.deleteShelfBo(shelf, hgabo);
                out.println("{\"success\":" + success + "}");
            } else if ("emptyData".equalsIgnoreCase(action)) {

                String shelf = request.getParameter("shelf");
                RoomManager rmg = new RoomManager();

                boolean success = rmg.emptyBo(shelf);
                out.println("{\"success\":" + success + "}");
            }



        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
